<?php 
/*************************************************************************************
 * testlogin.php
 * -----------
 * phpGMapTrips 
 * 
 * This php application allows you to easily record your travel destinations 
 * and display them with Google maps.
 * 
 * Author: Stephane Coste (lescoste@gmail.com)
 * Copyright: (c) 2006 Stephane Coste (http://lescoste.net/)
 * Release Version: 0.1
 * CVS Revision Version: $Revision: 1.1 $
 * Date Started: 2006/02/19
 * Last Modified: $Date: 2007/03/08 20:44:30 $
 *
 *************************************************************************************
 *
 *     This file is part of phpGMapTrips.
 *
 *   phpGMapTrips is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   phpGMapTrips is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with phpGMapTrips; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ************************************************************************************/
?>
<?PHP
error_reporting(E_ALL ^ E_NOTICE);
include_once("config.php");
$debugmessg = "";
function debug($m) {
global $debugmessg;
	//echo $m."<br/>\n";
	$debugmessg .= $m."<br/>\n";
}
// https support
if (getenv("HTTPS") == 'on') {
	$cfgUrl = 'https://';
} else {
	$cfgUrl = 'http://';
}
debug("cfgUrl=".$cfgUrl);

// getting other login variables
if ($message) $messageOld = $message;
debug ("messageOld=".$messageOld);
$message = false;
// choose between login or logout
if ($logout && !($_GET["logout"] || $_POST["logout"])) {
	// logout
	// phpversion = 4
	session_name("phpGMapTrips");
	session_start();
	unset($_SESSION['login']);
	unset($_SESSION['password']);
	session_destroy();
	$sessionPath = session_get_cookie_params(); 
	setcookie(session_name(), "", 0, $sessionPath["path"], $sessionPath["domain"]); 
	debug("logout.php");
} else {
	// loading login check
	debug("checkLogin");
	// loading functions and libraries
	$strUserNotExist = $strUserNotAllowed = $strPwNotFound = $strPwFalse = $strNoPassword = $strNoAccess;

	// make post variables global
	$entered_login = $_POST['entered_login'];
	$entered_password = $_POST['entered_password'];

	debug("entered_login=".$entered_login);
	debug("entered_password=".$entered_password);

	// check if login is necesary
	if (!$entered_login && !$entered_password) {
		debug("use data from session");
		// use data from session
		session_name("phpGMapTrips");
		session_start();
		$login = $_SESSION['login'];
		$password = $_SESSION['password'];
	} else {
		debug("use entered data");
		// use entered data
		session_name("phpGMapTrips");
		session_start();
		session_unregister("login");
		session_unregister("password");

		// encrypt entered login & password
		$login = $entered_login;
		if ($passwordEncryptedWithMD5 && function_exists(md5)) {
			$password = md5($entered_password);
		} else {
			$password = $entered_password;
		}
		$_SESSION['login'] = $login;
		$_SESSION['password'] = $password;
	}

	if (!$login) {
		debug(" no login available");
		include("interface.php");
echo $debugmessg;
		exit;
	}
	if (!$password) {
		debug(" no password available");
		$message = $strNoPassword;
		include("interface.php");
echo $debugmessg;
		exit;
	}

	$numLogin = count($cfgLogin);
	$userFound = false;
	debug(" check all the data input");
	for ($i = 1; $i <= $numLogin; $i++) {
		if ($cfgLogin[$i] != '' && $cfgLogin[$i] == $login) {
			debug(" user found --> check password");
			if ($cfgPassword[$i] == '' || $cfgPassword[$i] != $password) {
				debug(" password is wrong");
				$message = $strPwFalse;
				include("logout.php");
				include("interface.php");
echo $debugmessg;
				exit;
			}
			$userFound = true;
			$userNr = $i;
		}	}
	if ($userFound == false) {
		debug(" user is wrong");
		$message = $strUserNotExist;
		include("logout.php");
		include("interface.php");
echo $debugmessg;
		exit;
	}
	$userLevel = $cfgUserLevel[$userNr];
	$ID = $cfgUserID[$userNr];


	debug(" restore values");
	if ($dbOld) $db = $dbOld;
	if ($messageOld) $message = $messageOld;
}
echo $_GET["logout"];
?>
<html>
<head>
<link rel="stylesheet" href="gearth.css" type="text/css" media="screen" />
</head>
<body>
<?php echo $debugmessg; ?>
logged in
</body>
</html>
